package com.linkage.educloud.ah.view.fragment;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.linkage.educloud.ah.R;
import com.linkage.educloud.ah.bean.AccountData;
import com.linkage.educloud.ah.config.AccountConfig;
import com.linkage.educloud.ah.util.ImageLoader;
import com.linkage.educloud.ah.view.BasicFragment;
import com.linkage.educloud.ah.view.activity.login.LoginActivity;
import com.linkage.educloud.ah.view.activity.mine.AboutUsActivity;
import com.linkage.educloud.ah.view.activity.mine.ChangePwdActivity;
import com.linkage.educloud.ah.view.activity.mine.FeedbackActivity;
import com.linkage.educloud.ah.view.activity.mine.MyBagActivity;
import com.linkage.educloud.ah.view.activity.mine.PersonInfoActivity;
import com.linkage.educloud.ah.view.activity.mine.VClassActivity;
import com.linkage.educloud.ah.widget.CommonDialog;
import com.linkage.educloud.ah.widget.MyMore;
import com.uuzuche.lib_zxing.activity.CaptureActivity;
import com.uuzuche.lib_zxing.activity.CodeUtils;
import com.zzhou.common.library.util.ToastShow;

import butterknife.BindView;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 我的界面
 */

public class MineFragment extends BasicFragment implements View.OnClickListener {

    @BindView(R.id.fragment_mine_avater)
    CircleImageView fragment_mine_avater;
    @BindView(R.id.fragment_mine_name)
    TextView fragment_mine_name;
    @BindView(R.id.fragment_mine_uesrtype)
    TextView fragment_mine_uesrtype;
    @BindView(R.id.fragment_mine_renew)
    TextView fragment_mine_renew;
    @BindView(R.id.fragment_mine_sign)
    ImageView fragment_mine_sign;
    @BindView(R.id.fragment_mine_mymore_person)
    MyMore fragment_mine_mymore_person;
    @BindView(R.id.fragment_mine_mymore_mybag)
    MyMore fragment_mine_mymore_mybag;
    @BindView(R.id.fragment_mine_mymore_experience)
    MyMore fragment_mine_mymore_experience;
    @BindView(R.id.fragment_mine_mymore_changepwd)
    MyMore fragment_mine_mymore_changepwd;
    @BindView(R.id.fragment_mine_mymore_accountswitch)
    MyMore fragment_mine_mymore_accountswitch;
    @BindView(R.id.fragment_mine_mymore_scan)
    MyMore fragment_mine_mymore_scan;
    @BindView(R.id.fragment_mine_mymore_vclass)
    MyMore fragment_mine_mymore_vclass;
    @BindView(R.id.fragment_mine_mymore_feedback)
    MyMore fragment_mine_mymore_feedback;
    @BindView(R.id.fragment_mine_mymore_aboutus)
    MyMore fragment_mine_mymore_aboutus;
    @BindView(R.id.fragment_mine_mymore_versionupdate)
    MyMore fragment_mine_mymore_versionupdate;
    @BindView(R.id.fragment_mine_mymore_help)
    MyMore fragment_mine_mymore_help;
    @BindView(R.id.fragment_mine_mymore_introduce)
    MyMore fragment_mine_mymore_introduce;
    @BindView(R.id.fragment_mine_signout)
    Button fragment_mine_signout;

    AccountData accountData;
    private static final int SCAN_CODE = 11;

    public static MineFragment newInstance() {
        MineFragment fragment = new MineFragment();
        return fragment;
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.fragment_mine;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        accountData = AccountConfig.getLoginAccount();
        ImageLoader.loadAvatar(getActivity(), accountData.getAvatar(), fragment_mine_avater);
        fragment_mine_name.setText(accountData.getUserName());
        if (accountData.getUserType() == 1) {
            setUserType("教师", R.mipmap.ic_mine_teacher_icon);
        } else {
            setUserType("家长", R.mipmap.ic_mine_parent_icon);
        }
        fragment_mine_renew.setOnClickListener(this);
        fragment_mine_sign.setOnClickListener(this);
        fragment_mine_mymore_person.setOnClickListener(this);
        fragment_mine_mymore_mybag.setOnClickListener(this);
        fragment_mine_mymore_experience.setOnClickListener(this);
        fragment_mine_mymore_changepwd.setOnClickListener(this);
        fragment_mine_mymore_accountswitch.setOnClickListener(this);
        fragment_mine_mymore_scan.setOnClickListener(this);
        fragment_mine_mymore_vclass.setOnClickListener(this);
        fragment_mine_mymore_feedback.setOnClickListener(this);
        fragment_mine_mymore_aboutus.setOnClickListener(this);
        fragment_mine_mymore_versionupdate.setOnClickListener(this);
        fragment_mine_mymore_help.setOnClickListener(this);
        fragment_mine_mymore_introduce.setOnClickListener(this);
        fragment_mine_signout.setOnClickListener(this);
    }

    private void setUserType(String text, int resid) {
        fragment_mine_uesrtype.setText(text);
        Drawable drawable = getResources().getDrawable(resid);
        drawable.setBounds(0, 0, 35, 35);
        fragment_mine_uesrtype.setCompoundDrawables(drawable, null, null, null);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fragment_mine_renew:
                ToastShow.show(getActivity(), "待添加");
                break;
            case R.id.fragment_mine_sign:
                ToastShow.show(getActivity(), "待添加");
                break;
            case R.id.fragment_mine_mymore_person:
                PersonInfoActivity.launch(getActivity());
                break;
            case R.id.fragment_mine_mymore_mybag:
                MyBagActivity.launch(getActivity());
                break;
            case R.id.fragment_mine_mymore_experience:
//                /educloud/ucenter/experience/index
                break;
            case R.id.fragment_mine_mymore_changepwd:
                ChangePwdActivity.launch(getActivity());
                break;
            case R.id.fragment_mine_mymore_accountswitch:
                accountswitch_void();
                break;
            case R.id.fragment_mine_mymore_scan:
                Intent intent = new Intent(getActivity(), CaptureActivity.class);
                startActivityForResult(intent, SCAN_CODE);
                break;
            case R.id.fragment_mine_mymore_vclass:
                VClassActivity.launch(getActivity());
                break;
            case R.id.fragment_mine_mymore_feedback:
                FeedbackActivity.launch(getActivity());
                break;
            case R.id.fragment_mine_mymore_aboutus:
                AboutUsActivity.launch(getActivity());
                break;
            case R.id.fragment_mine_mymore_versionupdate:
                ToastShow.show(getActivity(), "待添加");
                break;
            case R.id.fragment_mine_mymore_help:
                help_void();
                break;
            case R.id.fragment_mine_mymore_introduce:
                ToastShow.show(getActivity(), "待添加");
                break;
            case R.id.fragment_mine_signout:
                signout_void();
                break;
        }
    }

    private void help_void() {
        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:10086"));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    private void signout_void() {
        final CommonDialog dialog = new CommonDialog(getActivity(), "温馨提示", "确认要退出吗？", "取消", "确认");
        dialog.setOkListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.setCancelListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    private void accountswitch_void() {
        final CommonDialog dialog = new CommonDialog(getActivity(), "温馨提示", "确认要切换帐号吗？", "取消", "确认");
        dialog.setOkListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
                dialog.dismiss();
            }
        });
        dialog.setCancelListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == SCAN_CODE) {
            if (null != data) {
                Bundle bundle = data.getExtras();
                if (bundle == null) {
                    return;
                }
                if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {
                    String result = bundle.getString(CodeUtils.RESULT_STRING);
                    ToastShow.show(getActivity(), result + "webview界面待添加");
                } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {
                    ToastShow.show(getActivity(), "解析二维码失败");
                }
            }
        }
    }
}